﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.BLL
{
    /// <summary>
    /// This is the business component to manage images
    /// </summary>
    public class ImagesService
    {
        IImages _images;

        /// <summary>
        /// Default constructor that will use the DAL set in the web config
        /// </summary>
        public ImagesService()
        {
            _images = MVCClubSite.DALFactory.DataAccess.CreateImages();
        }

        /// <summary>
        /// Constructor for testing with a different DAL
        /// </summary>
        /// <param name="announcements">DAL for the announcement service to use</param>
        public ImagesService(IImages images)
        {
            _images = images;
            if (_images == null)
                throw new InvalidOperationException("Images DAL cannot be null.");
        }

        /// <summary>
        /// Method to get Images
        /// </summary>
        /// <returns>A list of Images</returns>
        public List<Image> GetImages()
        {
            return _images.GetImages().ToList();
        }

        /// <summary>
        /// Method to an albums images
        /// </summary>
        /// <param name="id">ID of the album</param>
        /// <returns>A list of Images</returns>
        public List<Image> GetImagesByAlbumID(int id)
        {
            return _images.GetImagesByAlbumID(id).ToList();
        }

        /// <summary>
        /// Method to get an image id for an album thumbnail
        /// </summary>
        /// <param name="albumID">ID of the album</param>
        /// <returns>ID of the image</returns>
        public int GetAlbumThumbnailID(int albumID)
        {
            return _images.GetAlbumThumbnailID(albumID);
        }

        /// <summary>
        /// Method to get an image by id
        /// </summary>
        /// <param name="id">ID of the image to return</param>
        /// <returns>An image business entity</returns>
        public Image GetImageByID(int id)
        {
            return _images.GetImageByID(id);
        }

        /// <summary>
        /// Method to insert or update an image
        /// </summary>
        /// <param name="image">Image to insert or update</param>
        public void Save(Image image)
        {
            image.ThumbnailImage = ImageHelper.MakeThumbnail(image.OriginalImage, 70, 70);
            image.LargeImage = ImageHelper.ChangeSize(image.OriginalImage, 600);
            _images.Save(image);
        }

        /// <summary>
        /// Method to delete an image
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            _images.Delete(id);
        }

        /// <summary>
        /// Method to delete all images in an album
        /// </summary>
        /// <param name="ID">ID of the album</param>
        public void DeleteImagesByAlbumID(int id)
        {
            _images.DeleteByAlbumID(id);
        }
    }
}
